#ifndef TICKER_H
#define TICKER_H

#include <TimerEvent.h>

class TickerBase: public TimerEvent
{
public:
struct _model_function_object {
    
    struct _class;
    union
    {
        void (_class::*_method)();
        void (*_func)();
    };
    void *obj;
};
    TickerBase(const ticker_data_t *data);
    ~TickerBase()
    {
        detach();
    }
    void detach();

    void attach(void(*func)(),us_timestamp_t t)//ms
    {
        _store_obj.obj = nullptr;
        _store_obj._func = func;
        setup(t);
    }
    template<typename T,typename U>
    void attach(U *obj,void(T::*method)(),us_timestamp_t t)//ms
    {
        _store_obj.obj = obj;
        _store_obj._method = reinterpret_cast<decltype(_model_function_object::_method)>(method);

        setup(t);
    }
    template<typename T,typename U>
    void attach_absolute(U *obj,void(T::*method)(),us_timestamp_t abs_time)
    {
        _store_obj.obj = obj;
        _store_obj._method = reinterpret_cast<decltype(_model_function_object::_method)>(method);
        setup_absolute(abs_time);
    }

    void attach_absolute(void(*func)(),us_timestamp_t abs_time)
    {
        _store_obj.obj = nullptr;
        _store_obj._func = func;
        setup_absolute(abs_time);
    }

protected:
    void handler() override;
    us_timestamp_t  _us_delay{0};
    _model_function_object _store_obj;
private:
    void setup(us_timestamp_t t);
    void setup_absolute(us_timestamp_t t);




};


class Ticker : public TickerBase {
public:
    Ticker();
};
#endif